package com.hxkcloud.patient.aerotel.domain.health;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;


import com.hxkcloud.patient.aerotel.util.FileHelper;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by daipengfei on 16/3/11.
 */
public class HealthDeviceManager {

    private static Map<String, HealthDevice> devices = new HashMap<String, HealthDevice>();

    static {
        reload();
    }

    public static void reload() {
        devices.clear();
        Set<BluetoothDevice> bdvs = BluetoothAdapter.getDefaultAdapter().getBondedDevices();
        for (BluetoothDevice d : bdvs) {
            if (d.getName().contains("HVP")) {
                HealthDevice hd = new HealthDevice(d);
                add(hd);
            }
        }
    }

    public static void load(String filepath) throws IOException {
        FileHelper.readFileAsString(new File(filepath));
    }

    public static void save(String filepath) {
        FileHelper.write(filepath, "");
    }

    public static void add(HealthDevice device) {
        devices.put(device.getBtmac(), device);
    }

    public static HealthDevice get(String mac) {
        return devices.get(mac);
    }

    public static boolean exists(String mac) {
        HealthDevice d = get(mac);
        return d != null && d.isBond();
    }

    public static boolean exists(BluetoothDevice device) {
        HealthDevice d = get(device.getAddress());
        return d != null && d.isBond();
    }

    public static List<HealthDevice> getDevices() {
        List<HealthDevice> ds = new ArrayList<HealthDevice>();
        ds.addAll(devices.values());
        return ds;
    }

}
